<template>
  <div>
    <!-- 标题栏 -->
    <van-nav-bar title="出生日期"
                 fixed
                 @click-left="$router.back(-1)">
      <template #left>
        <img class="icon_right"
             src="@/assets/img/down_left.png"
             alt="">
      </template>
    </van-nav-bar>

    <!-- 盒子 -->
    <div class="box">
      <van-cell-group>
        <van-field v-model="birthday"
                   @click="dateShow = true"
                   @focus="fieldSelect"
                   label="出生日期"
                   center
                   clearable
                   placeholder="请选择出生日期" />
      </van-cell-group>
      <van-button @click="sub"
                  class="confirm"
                  type="info">确认修改</van-button>
    </div>

    <!-- 选择出生年月 -->
    <van-popup v-model="dateShow"
               round
               position="bottom">
      <van-datetime-picker v-model="currentDate"
                           type="date"
                           title="选择年月日"
                           :min-date="minDate"
                           :max-date="maxDate"
                           @cancel="dateShow = false"
                           @confirm="dateConfirm" />
    </van-popup>

  </div>
</template>

<script>
import dayjs from 'dayjs'
import { editPersonal } from '@/api/common'
export default {
  name: '',
  data () {
    return {
      birthday: '',
      dateShow: false,
      currentDate: new Date(),
      minDate: new Date(1850, 0, 1),
      maxDate: new Date(2022, 10, 1)
    }
  },
  created () {
    this.birthday = this.$route.query.v
  },
  computed: {
    userInfo () {
      return this.$store.getters.userInfo
    }
  },
  methods: {
    fieldSelect () {
      document.activeElement.blur()
    },
    dateConfirm (event) {
      this.birthday = dayjs(event).format('YYYY-MM-DD')
      this.dateShow = false
    },
    async sub () {
      const { code, msg } = await editPersonal({ birthday: this.birthday })
      if (code !== 1) return this.$toast.fail(msg)
      this.$toast.success(msg)
      // 本地回显 省略接口
      const _userInfo = this.userInfo
      _userInfo.birthday = this.birthday
      this.$store.commit('SET_USER_INFO', _userInfo)
      setTimeout(() => {
        this.$router.back()
      }, 1000)
    }
  }
}
</script>

<style scoped lang="less">
/deep/.van-cell {
  line-height: 34px;
  margin-bottom: 6px;
}
/deep/.van-cell__title {
  width: 75px;
  font-weight: 600;
}
.icon_right {
  width: 12px;
  height: 21px;
  vertical-align: middle;
}

.box {
  margin-top: 40px;
  padding-top: 10px;
  // padding: 10px 20px;
  background: #f7f8fa;
  height: calc(100vh - 50px);

  .confirm {
    margin-top: 10px;
    width: 80%;
    height: 48px;
    margin-top: 40px;
    background: linear-gradient(180deg, #3eb3ff 0%, #436eff 100%);
    box-shadow: 0px 8px 20px rgba(29, 90, 255, 0.32);
    border-radius: 37px;
    font-size: 16px;
    font-family: PingFang SC;
    font-weight: 600;
    line-height: 22px;
    margin-left: 10%;
    border: none;
    color: #ffffff;
  }
}
</style>
